package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.bigdata.fault.manager.EquipmentOverhualDetailManager;
import com.sduept.bigdata.fault.parser.entity.EquipmentOverhaulDetail;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.ledger.manager.SubstationLedgerManager;
import com.sduept.cim.ledger.model.SubstationLedger;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.DateUtil;
import com.sduept.utils.PingYinUtil;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.*;

@Named
@ViewScoped
public class EquipmentOverhualDetailController extends AbstractController {

	private static final long serialVersionUID = 6546410883479640179L;

	@Autowired
	private EquipmentOverhualDetailManager detailManager;
	@Autowired
	private SubstationLedgerManager stationLedgerM;
	@Autowired
	private CimManager cm;

	private List<EquipmentOverhaulDetail> details = new ArrayList<EquipmentOverhaulDetail>();

	private LazyDataModel<EquipmentOverhaulDetail> model;

	private EquipmentOverhaulDetail selectOverhaul;

	private Map<String, String> map = new HashMap<String, String>();// <变电站Id,调管机构>

	private List<String> desOrgs = new ArrayList<String>();

	private EquipmentOverhaulDetail current = new EquipmentOverhaulDetail();

	private int sort;// 排序所用

	private Date queryDate1;

	private Date queryDate2;

	private Date queryDate3;

	private Date queryDate4;
	private int recordSize;
	private String workPlace;//工作内容
	private String overhualContent;//检修内容

	@PostConstruct
	public void init() {
		initTable();
		initData();
	}

	private void initData() {
		Set<String> hashSet = new HashSet<String>();
		List<SubstationLedger> stationLedgerList = stationLedgerM.findAllSubstationLedger();
		if(stationLedgerList.size()>0) {
			for (SubstationLedger ledger : stationLedgerList) {
				map.put(ledger.getCimId(), ledger.getDispatchUnit());
				if(StringUtils.isNotEmpty(ledger.getDispatchUnit())) {
					hashSet.add(ledger.getDispatchUnit());
				}
			}
		}
		desOrgs.addAll(hashSet);
		Collections.sort(desOrgs, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return PingYinUtil.getPingYin(o1).compareTo(PingYinUtil.getPingYin(o2));
			}
		});
	}

	public void initTable() {
		final String start;
		final String end;
		final String start1;
		final String end1;
		if (null != queryDate1) {
			start = DateUtil.getStringDate(queryDate1, DateUtil.pattern_ymd) + " 00:00:00";
		} else {
			start = null;
		}
		if (null != queryDate2) {
			end = DateUtil.getStringDate(queryDate2, DateUtil.pattern_ymd) + " 23:59:59";
		} else {
			end = null;
		}
		if (null != queryDate1 && null != queryDate2 && queryDate1.getTime() > queryDate2.getTime()) {
			addErrorMessage("开始时间不能大于结束时间！");
			return;
		}
		if (null != queryDate3) {
			start1 = DateUtil.getStringDate(queryDate3, DateUtil.pattern_ymd) + " 00:00:00";
		} else {
			start1 = null;
		}
		if (null != queryDate4) {
			end1 = DateUtil.getStringDate(queryDate4, DateUtil.pattern_ymd) + " 23:59:59";
		} else {
			end1 = null;
		}
		if (null != queryDate3 && null != queryDate4 && queryDate3.getTime() > queryDate4.getTime()) {
			addErrorMessage("开始时间不能大于结束时间！");
			return;
		}
		model = new LazyDataModel<EquipmentOverhaulDetail>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -5725362047613389657L;

			@Override
			public List<EquipmentOverhaulDetail> load(int first, int pageSize, String sortField,
					SortOrder sortOrder, Map<String, Object> filters) {
				sort = first;
				details = detailManager.listEquipmentOverhaulDetailsWithRange(start, end, start1, end1,workPlace,overhualContent,
						new int[] { first, first + pageSize });
				return details;
			}

			@Override
			public List<EquipmentOverhaulDetail> load(int first, int pageSize, List<SortMeta> multiSortMeta,
					Map<String, Object> filters) {
				sort = first;
				details = detailManager.listEquipmentOverhaulDetailsWithRange(start, end, start1, end1,workPlace,overhualContent,
						new int[] { first, first + pageSize });
				return details;
			}

			@Override
			public EquipmentOverhaulDetail getRowData(String rowKey) {
				for (EquipmentOverhaulDetail t : details) {
					if (t.getId().equals(rowKey)) {
						return t;
					}
				}
				return null;
			}

			@Override
			public Object getRowKey(EquipmentOverhaulDetail object) {
				for (EquipmentOverhaulDetail t : details) {
					if (t.getId().equals(object.getId())) {
						return t;
					}
				}
				return null;
			}
		};
		recordSize = detailManager.countRecords(start, end, start1, end1,workPlace,overhualContent);
		model.setRowCount(recordSize);
	}

	/**
	 * 重置按钮调用方法
	 */
	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		queryDate3 = null;
		queryDate4 = null;
		workPlace = null;
		overhualContent = null;
		initTable();
		selectOverhaul = null;
	}

	public void preCreate() {
		current = new EquipmentOverhaulDetail();
	}

	public void preUpate() {
		current = selectOverhaul;
	}

	public void handleSelect(SelectEvent event) {
		Object item = event.getObject();
		String workplace = item.toString();
		if (StringUtils.isNotEmpty(workplace)) {
			List<Substation> stationByName = cm.getStationByName(current.getWorkingPlace());
			if (stationByName != null && stationByName.size() > 0) {
				current.setWorkingPlaceId(stationByName.get(0).getId());
			} else {
				List<ACLineSegment> acLineSegmentsByName = cm.getAcLineSegmentsByName(current
						.getWorkingPlace());
				if (acLineSegmentsByName != null && acLineSegmentsByName.size() > 0) {
					current.setWorkingPlaceId(acLineSegmentsByName.get(0).getId());
				}
			}
		}
		if (StringUtils.isNotEmpty(current.getWorkingPlaceId())) {
			current.setApplicationUnitId(map.get(current.getWorkingPlaceId()));
		}
	}

	public void save() {
		detailManager.saveEquipmentOverhaulDetail(current);
		initTable();
		addSuccessMessage("保存成功");
	}

	public void delete() {
		String id = selectOverhaul.getId();
		detailManager.deleteById(id);
		initTable();
		selectOverhaul = null;
		addSuccessMessage("删除成功");
	}

	public List<String> completeAppUnit(String query) {
		List<String> list = new ArrayList<String>();
		for (String s : desOrgs) {
			if (s.startsWith(query)) {
				list.add(s);
			}
		}
		return list;
	}

	public void onRowSelect(SelectEvent event) {
		selectOverhaul = (EquipmentOverhaulDetail) event.getObject();
	}
	public int getIndex(EquipmentOverhaulDetail detail) {
		return details.indexOf(detail) + sort + 1;
	}

	public List<EquipmentOverhaulDetail> getDetails() {
		return details;
	}

	public void setDetails(List<EquipmentOverhaulDetail> details) {
		this.details = details;
	}

	public LazyDataModel<EquipmentOverhaulDetail> getModel() {
		return model;
	}

	public void setModel(LazyDataModel<EquipmentOverhaulDetail> model) {
		this.model = model;
	}

	public EquipmentOverhaulDetail getSelectOverhaul() {
		return selectOverhaul;
	}

	public void setSelectOverhaul(EquipmentOverhaulDetail selectOverhaul) {
		this.selectOverhaul = selectOverhaul;
	}

	public EquipmentOverhaulDetail getCurrent() {
		return current;
	}

	public void setCurrent(EquipmentOverhaulDetail current) {
		this.current = current;
	}

	public int getSort() {
		return sort;
	}

	public void setSort(int sort) {
		this.sort = sort;
	}

	public Date getQueryDate1() {
		return queryDate1;
	}

	public void setQueryDate1(Date queryDate1) {
		this.queryDate1 = queryDate1;
	}

	public Date getQueryDate2() {
		return queryDate2;
	}

	public void setQueryDate2(Date queryDate2) {
		this.queryDate2 = queryDate2;
	}

	public Date getQueryDate3() {
		return queryDate3;
	}

	public void setQueryDate3(Date queryDate3) {
		this.queryDate3 = queryDate3;
	}

	public Date getQueryDate4() {
		return queryDate4;
	}

	public void setQueryDate4(Date queryDate4) {
		this.queryDate4 = queryDate4;
	}

	public int getRecordSize() {
		return recordSize;
	}

	public void setRecordSize(int recordSize) {
		this.recordSize = recordSize;
	}

	public String getWorkPlace() {
		return workPlace;
	}

	public void setWorkPlace(String workPlace) {
		this.workPlace = workPlace;
	}

	public String getOverhualContent() {
		return overhualContent;
	}

	public void setOverhualContent(String overhualContent) {
		this.overhualContent = overhualContent;
	}

}
